<?php
/*
* Input:  fieldspec, request (using CF_ParamReciever), Session
* return:  Filters object
* 
*
*/

class CF_FilterReciever {
	
var $filters;
	
	
	/**
	 * get_filter_vals() will recieve the filter variables from Session or request
     * and set them in the filters array.
     * To clear filter, pass in a request with the filter set to nothing
     * ($variable1=&variable2=)
     * Filters for each column are described in the fieldspec array 
     * fieldspec[<column>][filters]
	 * Where filters is an array of filters:  
	 * $filterspecarray[filterparam][op] = operation  ( others in spec, but not used here)
	 * This funciton flattens this array into one filters[] array, keyed by filterparam
	 * (filterparam must already be unique so it can be passed in requests)
	 * $this->filters[filterparam][var] = column variable to filter
	 * $this->filters[filterparam][var] = current value
	 * $this->filters[filterparam][op] = operation of filter, to later use in query (=,<,>, etc)	 
	 * $this->filters[filterparam][source] = source table or alias in query
	 * $this->filters[filterparam][add] = apply this filter to additions?  (default to 1)
	 * $this->filters[filterparam][filterhead] = (optinal) heading to show for filter when displayed in form
	 * Return a filter object from a fieldspec, filterprefix using get_persistant
	 * does request really work?  should probably be 'req' not $req
	 */
	static function get_filters($fieldspec,$method = 'req', $filterprefix = ''){
		if(is_array($fieldspec)){
		foreach($fieldspec AS $col_name => $colspec){
			$newspec = array();
			// check if filter needs table or alias source	
			if(isset($colspec['source'])){
				$tablesource = $colspec['source'] ;
			}else{
				$tablesource = null;
			}
			// check if filter is just set to 1.  if os, make proper spec array for =	
			if(!empty($colspec['filter'])){
				if(!is_array($colspec['filter'])){
					$newspec[$filterprefix . $col_name][op] = '=';
					$colspec['filter'] = $newspec;
				}
			
				foreach($colspec['filter'] AS $filterparam => $filterspec){
					$currentvalue = CF_Util::GetPersistent($filterparam,$method);
					$op = $filterspec['op'];
					if(!is_null($currentvalue)){
						$filter['var'] = $col_name;
						$filter['val'] = $currentvalue;
						$filter['op'] = $op;
						if(!is_null($tablesource)){
							$filter['source'] = $tablesource;
						}
						
						//print '<br />' . $col_name . 'FILTER  param: ' .$filterparam . ' variable: ' .  $this->filters[$filterparam]['var'] . ' Value: ' . $this->filters[$filterparam]['val'] . ' OP: ' . $this->filters[$filterparam]['op'];
					}else{
						unset($filter['var']);
						unset($filter['val']);
						unset($filter['op']);
						unset($filter['source']);
						
					}				
				$filters[$filterparam] = $filter;				
				}

			}
		}
		}
		return $filters;
	}
	// untested below
	//Set filter and Session value - generic	
	static function set_filter_val($col_name, $filter_value, &$filters, $filterprefix = ''){
		$filters[$filterprefix . $col_name]['var'] = $col_name;
		$filters[$filterprefix . $col_name]['val'] = $filter_value;
		$_SESSION[$filterprefix . $col_name] = $filter_value;
	}

	//UnSet filter and Session value - generic	
	static function clear_filter_val($col_name, &$filters, $filterprefix = ''){
		unset($filters[$filterprefix . $col_name]);
		unset($_SESSION[$filterprefix . $col_name]);
	}	
	
	// return an assoicative array ([<column namd>] = value) 
	static function filters_to_set($filters){
		$set = array();
		if(is_array($filters)){
		foreach($filters as $filter){
			if(isset($filter['val']) AND $filter['op'] = "="){
				$set[$filter['var']] = $filter['val'];
			}
		}
		}
	return $set;		
	}

}

?>